package com.ysd.dao.Impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ysd.entity.Scroll;
import com.ysd.util.DBManager;
import com.ysd.util.Result;

public class ScrollDaoImpl {
	//显示轮番图
	 public List<Scroll> getScrollList(){
			List<Scroll> list=new ArrayList<Scroll>();
			Statement st=null;
	    	ResultSet rs=null;
	    	Connection conn=DBManager.getConnection();//获得连接
	    	try {
				st=conn.createStatement();
				String sql="select * from scroll";//使用sql语句
				rs=st.executeQuery(sql);
				while(rs.next()) {//遍历结果集
					int scrollId=rs.getInt("scrollId");
					String scrollImg=rs.getString("scrollImg");
					Scroll scroll=new Scroll(scrollId,scrollImg);
					list.add(scroll);
				}
				return list;//正常情况下返回list集合
			} catch (SQLException e) {
				e.printStackTrace();
				return null;//异常情况下返回空
			}finally {//关闭 连接
				DBManager.closeAll(conn, st, rs);
			}
			 
		 }
	 //分页查询
	 public List<Scroll> getScrollListPage(int page,int size){
			List<Scroll> list=new ArrayList<Scroll>();
			Statement st=null;
	    	ResultSet rs=null;
	    	Connection conn=DBManager.getConnection();//获得连接
	    	try {
				st=conn.createStatement();
				String sql="SELECT * FROM scroll LIMIT "+(page-1)*size+","+size+"";//使用sql语句
				rs=st.executeQuery(sql);
				while(rs.next()) {//遍历结果集
					int scrollId=rs.getInt("scrollId");
					String scrollImg=rs.getString("scrollImg");
					Scroll scroll=new Scroll(scrollId,scrollImg);
					list.add(scroll);
				}
				return list;//正常情况下返回list集合
			} catch (SQLException e) {
				e.printStackTrace();
				return null;//异常情况下返回空
			}finally {//关闭 连接
				DBManager.closeAll(conn, st, rs);
			}
			 
		 }
	 
	 //删除轮番图
	 public int delScroll(int id) {
		 return DBManager.executeUpdate("DELETE FROM scroll WHERE scrollId="+id);
	 }
	 //添加轮播图
	 public int addScroll(Scroll s) {
		 return DBManager.executeUpdate("insert into scroll VALUES(null,'"+s.getScrollImg()+"')");
	 }
	 
	 //通过id获取
	 public Scroll getScrollById(int sId) {
		 Scroll s=null;
		 Connection conn=DBManager.getConnection();
		 Statement st=null;
		 ResultSet rs=null;
		 try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM scroll WHERE scrollId="+sId);
			while(rs.next()) {
				int scrollId=rs.getInt(1);
				String scrollImg=rs.getString(2);
				s=new Scroll(scrollId, scrollImg);
			}
			return s;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	 }
	 //修改轮播图
	 public int updateScrollById(Scroll s) {
		 return DBManager.executeUpdate("UPDATE scroll SET scrollImg='"+s.getScrollImg()+"' WHERE scrollId="+s.getScrollId()+"");
	 }
	 //批量删除
	 public int delectProductByIds(String ids) {
		 String sql="DELETE FROM scroll WHERE scrollId in("+ids+")";
		 System.out.println(sql);
		 return DBManager.executeUpdate(sql);
	}
	 
}
